﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using QASampling.BusinessObject;
using QASampling.Util;

namespace QASampling.UI
{
    public partial class frmQAEdit : BaseDialog
    {
        private static ToolTip tooltip;
        private string message;
        private string tableSource = string.Empty;
        public string TableSource
        {
            get { return tableSource; }
            set { tableSource = value; }
        }

        private string fieldName = string.Empty;
        public string FieldName
        {
            get { return fieldName; }
            set { fieldName = value; }
        }

        private string fieldValue = string.Empty;
        public string FieldValue
        {
            get { return fieldValue; }
            set { fieldValue = value; }
        }

        private string fieldLabel = string.Empty;
        public string FieldLabel
        {
            get { return fieldLabel; }
            set { fieldLabel = value; }
        }

        private int fieldLength = 20;
        public int FieldLength
        {
            get { return fieldLength; }
            set { fieldLength = value; }
        }

        private JenisField fieldType;
        public JenisField FieldType
        {
            get { return fieldType; }
            set { fieldType = value; }
        }

        private string dateFormat;
        public string DateFormat
        {
            get { return dateFormat; }
            set { dateFormat = value; }
        }

        private int digits = 0;
        public int Digits
        {
            get { return digits; }
            set { digits = value; }
        }

        public frmQAEdit()
        {
            InitializeComponent();
        }

        private void frmEditField_Load(object sender, EventArgs e)
        {
            tooltip = new ToolTip();
            tooltip.AutoPopDelay = 1000;
            tooltip.InitialDelay = 1000;
            tooltip.ReshowDelay = 500;
            if (fieldLength != 0)
                txtFieldValue.MaxLength = fieldLength;
            txtFieldValue.OriginalValue = fieldValue;
            txtFieldValue.Text = fieldValue;
            txtFieldValue.FieldType = fieldType;
            txtFieldValue.DateFormat = dateFormat;
            txtFieldValue.LabelText = fieldLabel;
            txtFieldValue.Digits = digits;
            if (fieldType == JenisField.Currency)
                txtFieldValue.TextAlign = HorizontalAlignment.Right;
            lblField.Text = fieldLabel;
            if (fieldType == JenisField.Datetime)
                lblField.Text += " ( " + dateFormat + " ) ";
            cmbAlasanQA.DataSource = QAController.GetAlasanQA();
            cmbAlasanQA.SelectedIndex = 0;
            txtFieldValue.Select();
        }

        private void btnSimpan_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.ValidateChildren() || !GetValidation())
                    return;
                string fieldBefore = fieldValue;
                fieldValue = txtFieldValue.Text;

                if (!fieldBefore.Equals(fieldValue)) //added by Vincent for the IF VALUE CHANGED 20130131
                {
                    string value = ValidationUtil.ValidatingField(this);
                    
                    //modified by adding if by Vincent 20130201 for handling , when insert database
                    if (digits>0)
                    {
                        QAController.UpdateField(fieldName, value.Replace(",","."), tableSource);
                    }
                    else
                    {
                        QAController.UpdateField(fieldName, value, tableSource);
                    }

                    QAChangeLog qa = new QAChangeLog();
                    qa.FieldAction = JenisCommand.Update.ToString();
                    qa.FieldTable = tableSource;
                    qa.FieldChanged = fieldName;
                    qa.FieldValue = fieldValue;
                    qa.FieldBefore = fieldBefore;
                    qa.Comments = cmbAlasanQA.SelectedValue.ToString();
                    QAController.TambahQALog(qa);
                }
                
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void txtFieldValue_Validating(object sender, CancelEventArgs e)
        {
            message = string.Empty;
            if (fieldType.Equals(JenisField.Datetime))
            {
                if (!ValidationUtil.IsValidDate(txtFieldValue.Text, dateFormat))
                {
                    txtFieldValue.BackColor = Color.Red;
                    message = "Masukkan penanggalan dengan benar " + dateFormat;
                }
            }
            else if (fieldType.Equals(JenisField.Currency))
            {
                if (!ValidationUtil.IsValidCurrency(txtFieldValue.Text, true))
                {
                    txtFieldValue.BackColor = Color.Red;
                    message = "Masukkan penulisan uang dengan benar";
                }
            }
        }

        /*private void txtFieldValue_TextChanged(object sender, EventArgs e)
        {
            txtFieldValue.BackColor = Color.LightGreen;
            tooltip.Hide(txtFieldValue);
        }

        private void txtFieldValue_MouseHover(object sender, EventArgs e)
        {
            //tooltip.SetToolTip(txtFieldValue, message);
            //tooltip.Show(message, txtFieldValue, 10, 15, 2000);
        }

        private void txtFieldValue_MouseLeave(object sender, EventArgs e)
        {
            tooltip.Hide(txtFieldValue);
        }*/

        private bool GetValidation()
        {
            return string.IsNullOrEmpty(txtFieldValue.ErrorMsg);
        }

        private void txtFieldValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            //added by Vincent 20130131
            if (((int)e.KeyChar)==13)
            {
                //added by Vincent 20130208 for auto uppercase
                if (!txtFieldValue.Text.Trim().Equals(""))
                {
                    txtFieldValue.Text = txtFieldValue.Text.ToUpper();
                }
                //--------------------------------------------
                btnSimpan.PerformClick();
            }
            //-----------------------
        }

        private void cmbAlasanQA_KeyPress(object sender, KeyPressEventArgs e)
        {
            //added by Vincent 20130131
            if (((int)e.KeyChar) == 13)
            {
                //added by Vincent 20130208 for auto uppercase
                if (!txtFieldValue.Text.Trim().Equals(""))
                {
                    txtFieldValue.Text = txtFieldValue.Text.ToUpper();
                }
                //--------------------------------------------
                btnSimpan.PerformClick();
            }
            //-----------------------
        }

        private void txtFieldValue_Leave(object sender, EventArgs e)
        {
            //added by Vincent 20130208 for auto uppercase
            if (!txtFieldValue.Text.Trim().Equals(""))
            {
                txtFieldValue.Text = txtFieldValue.Text.ToUpper();    
            }
            //--------------------------------------------
        }

    }
}
